package com.jianggujin.zkgui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

public class NodeValuePanel extends JSplitPane {
    private JTextArea nodePath;
    private JCheckBox nodePathURLDecode;
    private JButton nodePathDubboDecode;
    private JTextArea nodeValue;
    private JCheckBox nodeValueURLDecode;
    private JButton nodeValueSave;
    private JButton nodeValueNull;
    private MainFrame mainFrame;
    private String path;

    public NodeValuePanel(MainFrame mainFrame) {
        super(VERTICAL_SPLIT);
        this.mainFrame = mainFrame;
        initComponent();
    }

    private void initComponent() {
        initLeftComponent();
        initRightComponent();
    }

    private void initLeftComponent() {
        JPanel pathPanel = new JPanel(new BorderLayout());
        nodePath = new JTextArea();
        nodePath.setEditable(false);
        nodePath.setLineWrap(true);
        pathPanel.add(new JScrollPane(nodePath));

        JPanel pathBottomPanel = new JPanel();
        nodePathURLDecode = new JCheckBox("URL解码");
        pathBottomPanel.add(nodePathURLDecode);
        nodePathURLDecode.addActionListener(e -> {
            if (nodePathURLDecode.isSelected()) {
                nodePath.setText(decode(nodePath.getText()));
            } else {
                nodePath.setText(encode(nodePath.getText()));
            }
        });
        nodePathDubboDecode = new JButton("Dubbo解码");
        pathBottomPanel.add(nodePathDubboDecode);
        nodePathDubboDecode.addActionListener(e -> {
            DubboDialog.showDialog(this.mainFrame, this.path);
        });
        pathPanel.add(pathBottomPanel, BorderLayout.SOUTH);
        pathPanel.setBorder(BorderFactory.createTitledBorder("节点路径"));
        setLeftComponent(pathPanel);
    }

    private void initRightComponent() {
        JPanel valuePanel = new JPanel(new BorderLayout());
        nodeValue = new JTextArea();
        nodeValue.setLineWrap(true);
        valuePanel.add(new JScrollPane(nodeValue));

        JPanel nodeBottomPanel = new JPanel();
        nodeValueURLDecode = new JCheckBox("URL解码");
        nodeBottomPanel.add(nodeValueURLDecode);
        nodeValueURLDecode.addActionListener(e -> {
            if (nodeValueURLDecode.isSelected()) {
                nodeValue.setText(decode(nodeValue.getText()));
            } else {
                nodeValue.setText(encode(nodeValue.getText()));
            }
        });
        nodeValueSave = new JButton("保存");
        nodeValueSave.addActionListener(e -> {
            mainFrame.saveNodeValue(path, nodeValue.getText());
        });
        nodeBottomPanel.add(nodeValueSave);
        nodeValueNull = new JButton("设置NULL");
        nodeValueNull.addActionListener(e -> {
            mainFrame.saveNodeValue(path, null);
        });
        nodeBottomPanel.add(nodeValueNull);
        valuePanel.add(nodeBottomPanel, BorderLayout.SOUTH);
        valuePanel.setBorder(BorderFactory.createTitledBorder("节点数据"));

        setRightComponent(valuePanel);
    }

    private String encode(String value) {
        try {
            return URLEncoder.encode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
        }
        return value;
    }

    private String decode(String value) {
        try {
            return URLDecoder.decode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
        }
        return value;
    }

    public void clear() {
        this.path = "";
        nodePath.setText(path);
        nodeValue.setText("");
    }

    public void showNodeInfo(String path, byte[] data) {
        this.path = path;
        if (nodePathURLDecode.isSelected()) {
            path = decode(path);
        }
        nodePath.setText(path);
        if (data != null) {
            nodeValue.setForeground(Color.black);
            String value = null;
            try {
                value = new String(data, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // ignore
            }
            if (nodeValueURLDecode.isSelected()) {
                value = decode(value);
            }
            nodeValue.setText(value);
        } else {
            nodeValue.setForeground(Color.GRAY);
            nodeValue.setText("NULL");
        }
    }

    public String getPath() {
        return path;
    }
}
